//
//  ProjectViewController.m
//  PASystem
//
//  项目立项
//

#import "ProjectViewController.h"
#import "ProjectFirstModel.h"

#define DEVICEWIDTH [UIScreen mainScreen].bounds.size.width
#define DEVICEHEIGHT [UIScreen mainScreen].bounds.size.height
@interface ProjectViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) NSMutableArray *dataArray2;

@property (nonatomic,strong) UITableView *tab1;

@property (nonatomic,strong) UITableView *tab2;


@end

@implementation ProjectViewController

- (NSMutableArray *)dataArray2{
    if (_dataArray2 == nil) {
        
        _dataArray2 = [NSMutableArray array];
    }
    return _dataArray2;
}

-(NSMutableArray *)dataArray{

    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:241/255.0 green:242/255.0 blue:243/255.0 alpha:1];
    
    UITableView *tab1 = [[UITableView alloc] init];
    self.tab1 = tab1;
    tab1.delegate = self;
    tab1.dataSource = self;
    tab1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tab1];
    [tab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).mas_offset(10);
        make.left.equalTo(self.view.mas_left).mas_offset(10);
        make.right.equalTo(self.view.mas_right).mas_offset(-10);
        make.height.equalTo(@200);
    }];
    
    UITableView *tab2 = [[UITableView alloc] init];
    self.tab2 = tab2;
    tab2.delegate = self;
    tab2.dataSource = self;
    tab2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tab2];
    [tab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tab1.mas_bottom).mas_offset(10);
        make.left.equalTo(self.view.mas_left).mas_offset(10);
        make.right.equalTo(self.view.mas_right).mas_offset(-10);
        make.bottom.equalTo(self.view.mas_bottom).mas_offset(-168);
    }];
    
    [self loadData];

    [self loadData2];
}

- (void)loadData{
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];

    NSString *json  = [NSString stringWithFormat:@"{'action': dxmMch,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@}'}",userId,pwdId,userHumber,self.ID];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        [self.dataArray removeAllObjects];

        NSDictionary *dict = JSON[@"reason"];
        NSDictionary *dict2 = dict[@"attrs"];

        if (dict2) {
            ProjectFirstModel *model = [ProjectFirstModel mj_objectWithKeyValues:dict2];
            
            [self.dataArray addObject:model];

        }
        [self.tab1 reloadData];

        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}

-(void)loadData2{
 
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': xmlxMch,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@}'}",userId,pwdId,userHumber,self.ID];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        NSDictionary *dict = JSON[@"reason"];

        if (dict) {
            
            NSDictionary *dict2 = dict[@"attrs"];
            
            ProjectFirstModel *model = [ProjectFirstModel mj_objectWithKeyValues:dict2];
            
            [self.dataArray2 addObject:model];
        }
        
        [self.tab2 reloadData];

        
    } failure:^(NSError *error) {
        
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (tableView == self.tab1) {
        
        return 5;
        
    }else{
        return 6;
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.tab1) {
        ProjectFirstModel *model = self.dataArray.firstObject;
        if (indexPath.row == 0) {
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10,80, 40)];
            
            label.text = @"举办名称:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.PROJECTNAME;
            lbl2.textAlignment = NSTextAlignmentRight;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right).mas_offset(-5);
            }];
            
            return cell;
            
        }else if (indexPath.row == 1){
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 40)];
            
            label.text = @"主办机构:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            lbl2.textAlignment = NSTextAlignmentRight;
            
            lbl2.text = model.ORGANIZER;
            
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right).mas_offset(-5);
            }];
            
            
            return cell;
            
            
        }
        else if (indexPath.row == 2){
            
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 40)];
            
            label.text = @"举办地:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.PROJECTADDRESS;
            lbl2.textAlignment = NSTextAlignmentRight;
            
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right).mas_offset(-5);
            }];
            
            
            return cell;
            
            
        }
        else if (indexPath.row == 3){
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10,80, 40)];
            
            label.text = @"展会时间:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            //时间戳
            NSString *string = model.PROJECTTIME;
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            fmt.dateFormat = @"MMM d, yyyy HH:mm:ss aa";
            fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-US"];
            NSDate *date = [fmt dateFromString:string];
            NSDateFormatter *fm = [[NSDateFormatter alloc] init];
            fm.dateFormat = @"yyyy-MM-dd";
            NSString *selfYear = [fm stringFromDate:date];
            
            lbl2.text = selfYear;
            
            
            lbl2.textAlignment = NSTextAlignmentRight;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right).mas_offset(-5);
            }];
            
            return cell;
            
        }
        else if (indexPath.row == 4){
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 40)];
            label.text = @"项目简介:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            [lbl2 setNumberOfLines:0];
            lbl2.font = [UIFont systemFontOfSize:15];
            lbl2.textAlignment = NSTextAlignmentLeft;
            lbl2.text = model.REMARK;
            [cell.contentView addSubview:lbl2];
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(cell.contentView.mas_top).mas_offset(10);
                make.left.equalTo(label.mas_right).mas_offset(5);
                make.right.equalTo(cell.contentView.mas_right).mas_offset(-5);
                
            }];
            
            return cell;
        }else{
            
            return nil;
        }
    }else{
        ProjectFirstModel *model = self.dataArray2.firstObject;
        if (indexPath.row == 0) {
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10,80, 40)];
            
            label.text = @"项目名称:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.NAME;
            lbl2.textAlignment = NSTextAlignmentRight;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right).mas_offset(-5);
            }];
            
            return cell;
            
        }else if (indexPath.row == 1){
            
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 40)];
            
            label.text = @"立项时间:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            //时间戳
            NSString *string = model.REPLYTIME;
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            fmt.dateFormat = @"MMM d, yyyy HH:mm:ss aa";
            fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-US"];
            NSDate *date = [fmt dateFromString:string];
            NSDateFormatter *fm = [[NSDateFormatter alloc] init];
            fm.dateFormat = @"yyyy-MM-dd";
            NSString *selfYear = [fm stringFromDate:date];
            lbl2.text = selfYear;
            
            lbl2.textAlignment = NSTextAlignmentRight;
            
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right).mas_offset(-5);
            }];
            
            
            return cell;
            
            
        }
        else if (indexPath.row == 2){
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10,80, 40)];
            
            label.text = @"预算金额:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.MONEY;
            
            lbl2.textAlignment = NSTextAlignmentRight;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right).mas_offset(-5);
            }];
            
            return cell;
            
        }
        else if (indexPath.row == 3){
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 40)];
            label.text = @"批复金额:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            lbl2.textAlignment = NSTextAlignmentRight;
            lbl2.text = model.REPLYMONEY;
            [cell.contentView addSubview:lbl2];
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right).mas_offset(-5);
            }];
            
            return cell;
        }else if (indexPath.row == 4){
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 40)];
            label.text = @"批复时间:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            lbl2.textAlignment = NSTextAlignmentRight;
            //时间戳
            NSString *string = model.REPLYTIME;
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            fmt.dateFormat = @"MMM d, yyyy HH:mm:ss aa";
            fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-US"];
            NSDate *date = [fmt dateFromString:string];
            NSDateFormatter *fm = [[NSDateFormatter alloc] init];
            fm.dateFormat = @"yyyy-MM-dd";
            NSString *selfYear = [fm stringFromDate:date];
            lbl2.text = selfYear;
            
            [cell.contentView addSubview:lbl2];
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right).mas_offset(-5);
            }];
            
            return cell;
        }else if (indexPath.row == 5){
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
            label.text = @"备注:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            lbl2.textAlignment = NSTextAlignmentRight;
            lbl2.text = model.REMARKS;
            [cell.contentView addSubview:lbl2];
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right).mas_offset(-5);
            }];
            
            return cell;
        }else{
            
            return nil;
        }
    
        return nil;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProjectFirstModel *model = self.dataArray.firstObject;

    if (tableView == self.tab1) {
        
        if (indexPath.row == 4) {
            
            CGFloat H = [OAToolBox sizebyText:model.REMARK font:15 minsWidth:80];
            
            return H + 50;
        }else{
        
            return 60;
        }
        
        
    }else{
        
        if (indexPath.row == 5) {
            
            CGFloat H = [OAToolBox sizebyText:model.REMARKS font:15 minsWidth:DEVICEWIDTH-100];
            
            return H + 50;
        }else{
            
            return 60;
        }
        
    }


}



@end















